package cn.myblog.ainblog.component;

import cn.myblog.ainblog.model.LogModel;
import cn.myblog.ainblog.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

/**
 * @author Dovaior
 * @Description 日志aop
 * @create 2021-05-03 11:09
 **/
@Aspect
@Component
public class WebLogAspect {
    private final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);

    @Autowired
    private LogService logService;

    @Pointcut("execution(* cn.myblog.ainblog.service.*.add*(..))")//切入点描述add开头的
    public void addLog(){}//签名，可以理解成这个切入点的一个名称

    @Pointcut("execution(* cn.myblog.ainblog.service.*.update*(..))")//切入点描述update开头的
    public void updateLog(){}

    @Pointcut("execution(* cn.myblog.ainblog.service.*.delete*(..))")//切入点描述delete开头的
    public void deleteLog(){}

    @Before("addLog()||updateLog()||deleteLog()") //在切入点的方法run之前要干的,监听增删改
    public void logBeforeController(JoinPoint joinPoint) {
//        这个RequestContextHolder是Springmvc提供来获得请求的东西
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
//         记录下请求内容
//        logger.info("################URL : " + request.getRequestURL().toString());
//        logger.info("################HTTP_METHOD : " + request.getMethod());
//        logger.info("################IP : " + request.getRemoteAddr());
//        logger.info("################THE ARGS OF THE CONTROLLER : " + Arrays.toString(joinPoint.getArgs()));
//        下面这个getSignature().getDeclaringTypeName()是获取包+类名的   然后后面的joinPoint.getSignature.getName()获取了方法名
//        logger.info("################CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        LogModel logModel = new LogModel();
        //获取路径
        String log = joinPoint.getSignature().getDeclaringTypeName();
        //获取具体方法名
        String logName = joinPoint.getSignature().getName();
        //为执行的方法赋值  /例：cn.myblog.ainblog.service.xxxService.xxxxx
        logModel.setLogMethod(log+"."+logName);
        //前半句话
        String firstHalf = "";
        String[] split = log.split("\\.");
        switch (split[4]){
            case "UserService":
                firstHalf = "对用户进行了";
                break;
            case "TagService":
                firstHalf = "对标签进行了";
                break;
            case "CategoryService":
                firstHalf = "对分类进行了";
                break;
            case "ArticleService":
                firstHalf = "对博文进行了";
                break;
            case "CommentService":
                firstHalf = "对留言进行了";
                break;
        }
        String secondHalf = "";
        switch (logName.substring(0,3)){
            case "add":
                secondHalf = "添加操作。";
                break;
            case "upd":
                secondHalf = "更新操作。";
                break;
            case "del":
                secondHalf = "删除操作。";
                break;
        }
        String IPaddress = request.getRemoteAddr();
        String urlMethod = request.getMethod();
        logModel.setLogDescription(firstHalf+","+secondHalf+","+IPaddress+","+urlMethod+","+request.getRequestURL().toString());
        logModel.setLogDate(new Date());
        logService.insertLog(logModel);
    }
}
